# -*- coding: utf-8 -*-

import imp
import os
import errno


def make_dir(dname):
    if not os.path.isdir(dname):
        os.makedirs(dname)


class DefaultConfig():
    SQLALCHEMY_DATABASE_URI = 'mysql+mysqlconnector://root:root@localhost:3306/auto_test?charset=utf8'


class Config(dict):
    def __init__(self, defaults=None):
        dict.__init__(self, defaults or {})

    def config_from_object(self, obj):
        for key in dir(obj):
            if key.isupper():
                self[key] = getattr(obj, key)

    def config_from_py(self, filename):
        d = imp.new_module('config')
        d.__file__ = filename
        try:
            with open(filename) as config_file:
                exec (compile(config_file.read(), filename, 'exec'), d.__dict__)
        except IOError as e:
            if e.errno in (errno.ENOENT, errno.EISDIR):
                return False
            e.strerror = 'Unable to load configuration file (%s)' % e.strerror
            raise
        self.config_from_object(d)
        return True


config = Config()
config.config_from_object(DefaultConfig())
config.config_from_py("/Users/inke/ws/python/monkey/config/production.py")

if __name__ == '__main__':
    print(config)
